package com.gui;

import com.manage.ClassRoom;
import com.virtual.Run;
import javax.swing.*;
import java.awt.*;

public class ClassManagementPanel extends JPanel {
    private Run systemManager;
    private JList<ClassRoom> classList;
    private DefaultListModel<ClassRoom> listModel;
    
    public ClassManagementPanel(Run systemManager) {
        this.systemManager = systemManager;
        initializeUI();
        updateClassList();
    }
    
    private void initializeUI() {
        setLayout(new BorderLayout());
        
        // 创建列表
        listModel = new DefaultListModel<>();
        classList = new JList<>(listModel);
        JScrollPane scrollPane = new JScrollPane(classList);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("新增班级");
        JButton editButton = new JButton("编辑班级");
        JButton deleteButton = new JButton("删除班级");
        
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);
        
        // 添加事件监听
        addButton.addActionListener(e -> showAddClassDialog());
        editButton.addActionListener(e -> editSelectedClass());
        deleteButton.addActionListener(e -> deleteSelectedClass());
        
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
        
        // 设置列表选择模式
        classList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    private void showAddClassDialog() {
        String className = JOptionPane.showInputDialog(this, "请输入班级名称：");
        if (className != null && !className.trim().isEmpty()) {
            ClassRoom newClass = new ClassRoom(generateClassId(), className);
            systemManager.addClass(newClass);
            updateClassList();
            classList.setSelectedValue(newClass, true);
        }
    }
    
    private void editSelectedClass() {
        ClassRoom selectedClass = classList.getSelectedValue();
        if (selectedClass != null) {
            String newName = JOptionPane.showInputDialog(this, 
                "请输入新的班级名称：", selectedClass.getName());
            if (newName != null && !newName.trim().isEmpty()) {
                selectedClass.setName(newName);
                updateClassList();
            }
        }
    }
    
    private void deleteSelectedClass() {
        ClassRoom selectedClass = classList.getSelectedValue();
        if (selectedClass != null) {
            int confirm = JOptionPane.showConfirmDialog(this, 
                "确定要删除该班级吗？", "确认删除", 
                JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                systemManager.removeClass(selectedClass);
                updateClassList();
            }
        }
    }
    
    private void updateClassList() {
        listModel.clear();
        for (ClassRoom class_ : systemManager.getClasses()) {
            listModel.addElement(class_);
        }
    }
    
    private int generateClassId() {
        // 简单的ID生成方法，实际应用中可能需要更复杂的逻辑
        return systemManager.getClasses().size() + 1;
    }
} 